n = int(input())

r_dict = {'rat':1,'woman':2,'child':2,'man':3,'captain':4}

data= []

for i in range(n):
    data.append(input().split())

data.sort(key=lambda x:r_dict.get(x[-1]))

for d in data:
    print(d[0])
